#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-09-07 8:44 上午
# @Author  : yangdy
# @File    : 2020-09-07.347.前K个高频元素
# @Software: PyCharmCE
from tools.my_methods import *


class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        n = len(nums)
        cc = []
        nums.sort()
        c = 1
        for i in range(1, n):
            if nums[i] != nums[i-1]:
                cc.append((c, nums[i-1]))
                c = 1
            else:
                c += 1
        cc.append((c, nums[n-1]))
        print(cc)
        # sorted(cc, key=lambda x: -x[0])
        # sorted(cc)
        print(cc)
        cc.sort(key=lambda x: x[0], reverse=True)
        print(cc)

        ans = []
        for ci in cc:
            if k > 0:
                ans.append(ci[1])
                k -= 1
        return ans


questions = [
    ([1, 1, 1, 2, 2, 3], 2),
    ([1, ], 1),
    ([1, 2], 2),
    ([4, 1, -1, 2, -1, 2, 3], 2),
]

work(questions, lambda q: Solution().topKFrequent(q[0], q[1]))
